local skel = fk.CreateSkill {
  name = "emo__tongqi",
}

Fk:loadTranslationTable{
  ["emo__tongqi"] = "通奇",
  [":emo__tongqi"] = "出牌阶段限一次，你可以将任意张【杀】和【闪】置于武将牌上（可如手牌使用打出），获得等量张锦囊牌/装备牌。锦囊牌对你生效前，你可以重铸武将牌上【杀】和【闪】各一张，视为使用【无懈可击】。",

  ["#emo__tongqi-put"] = "可以将任意张【杀】和【闪】置于武将牌上",
  ["#emo__tongqi-nullification"] = "你可以重铸【杀】和【闪】各一张，视为对 %arg 使用【无懈可击】",
  ["emo__tongqi_select"] = "通奇",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  min_card_num = 1,
  target_num = 0,
  target_filter = Util.FalseFunc,
  derived_piles = skel.name,
  prompt = "#emo__tongqi-put",
  card_filter = function (self, player, to_select)
    return Fk:getCardById(to_select, true).trueName == "slash" or Fk:getCardById(to_select, true).trueName == "jink"
  end,
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryPhase)
  end,
  can_use = function (self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) < 1
  end,
  on_use = function (self, room, use)
    local cards, player = use.cards, use.from
    local trick, equip = 0, 0
    for _, id in ipairs(cards) do
      if Fk:getCardById(id, true).trueName == "slash" then
        trick = trick + 1
      else
        equip = equip + 1
      end
    end
    player:addToPile(skel.name, cards, true, skel.name)
    if player.dead then return end
    local get = {}
    if trick > 0 then
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|trick", trick, "allPiles")
      table.insertTable(get, ids)
    end
    if equip > 0 then
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|equip", equip, "allPiles")
      table.insertTable(get, ids)
    end
    if #get > 0 then
      room:obtainCard(player, get, true, fk.ReasonJustMove, player, skel.name)
    end
  end,
})

skel:addEffect(fk.BeforeCardEffect, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and data.to == player and data.card.type == Card.TypeTrick
     and not data:isDisresponsive(player) and not data:isUnoffsetable(player) and not player:prohibitUse(Fk:cloneCard("nullification")) then
      local slash, jink
      for _, id in ipairs(player:getPile(skel.name)) do
        if Fk:getCardById(id, true).trueName == "slash" then
          slash = true
        else
          jink = true
        end
      end
      return slash and jink
    end
  end,
  on_cost = function (self, event, target, player, data)
    local _, dat = player.room:askToUseActiveSkill(player, { skill_name = "emo__tongqi_select",
    prompt = "#emo__tongqi-nullification:::"..data.card:toLogString() })
    if dat then
      event:setCostData(self, dat.cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:recastCard(event:getCostData(self), player, skel.name)
    local card = Fk:cloneCard("nullification")
    card.skillName = skel.name
    room:useCard{
      from = player, tos = {}, card = card,
      toCard = data.card, responseToEvent = data,
    }
  end,
})

skel:addEffect("filter", {
  handly_cards = function (self, player)
    if player:hasSkill(skel.name) then
      return player:getPile(skel.name)
    end
  end,
})

local skel2 = fk.CreateSkill {
  name = "emo__tongqi_select",
}

skel2:addEffect("active", {
  card_num = 2,
  target_num = 0,
  expand_pile = skel.name,
  target_filter = Util.FalseFunc,
  card_filter = function (self, player, to_select, selected)
    if #selected > 1 or player:getPileNameOfId(to_select) ~= skel.name then return false end
    local name = Fk:getCardById(to_select, true).trueName
    if name == "slash" or name == "jink" then
      if #selected == 0 then return true end
      local first = Fk:getCardById(selected[1], true).trueName
      return name ~= first
    end
  end,
})

return {skel, skel2}
